package com.yh.modules.data.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.common.annotation.DataFilter;
import com.yh.common.constant.Constant;
import com.yh.common.constant.StatusEnums;
import com.yh.common.exception.RRException;
import com.yh.common.utils.DataFilterUtil;
import com.yh.common.utils.PageUtils;
import com.yh.common.utils.Query;
import com.yh.modules.data.dao.ProjectDao;
import com.yh.modules.data.entity.ProjectEntity;
import com.yh.modules.data.service.ProjectService;
import com.yh.modules.sys.entity.SysDeptEntity;
import com.yh.modules.sys.service.SysDeptService;
import com.yh.shiro.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("projectService")
@Slf4j
public class ProjectServiceImpl extends ServiceImpl<ProjectDao, ProjectEntity> implements ProjectService {

    @Autowired
    private SysDeptService sysDeptService;

    @Override
    @DataFilter(subDept = true, user = false)
    public PageUtils queryPage(Map<String, Object> params) {
        String projectName = MapUtil.getStr(params, "projectName");
        Integer deptId = DataFilterUtil.getDeptId(params);

        EntityWrapper<ProjectEntity> wrapper = new EntityWrapper<>();
        wrapper.like(StrUtil.isNotBlank(projectName), "name", projectName);
        wrapper.eq(ObjectUtil.isNotNull(deptId), "dept_id", deptId);
        wrapper.addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String) params.get(Constant.SQL_FILTER));

        Page<ProjectEntity> page = this.selectPage(new Query<ProjectEntity>(params).getPage(), wrapper);

        //设置部门名称
        List<ProjectEntity> records = page.getRecords();
        for (ProjectEntity project : records) {
            SysDeptEntity deptEntity = sysDeptService.selectById(project.getDeptId());
            project.setDeptName(deptEntity.getName());
        }

        return new PageUtils(page);
    }


    @Override
    public void save(ProjectEntity project) {
        checkProjectNameRepeat(project);

        project.setStatus(StatusEnums.ENABLE.getCode());
        project.setCreateTime(new Date());
        project.setUpdateTime(new Date());
        this.insert(project);
    }

    @Override
    public void update(ProjectEntity project) {
        checkProjectNameRepeat(project);

        project.setUpdateTime(new Date());
        this.updateById(project);
    }

    @Override
    public List<ProjectEntity> getProjectListByDeptId(Integer deptId) {
        EntityWrapper<ProjectEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("dept_id", deptId);

        List<ProjectEntity> projectEntityList = this.selectList(wrapper);

        return projectEntityList;
    }


    private void checkProjectNameRepeat(ProjectEntity project) {
        EntityWrapper<ProjectEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("dept_id", project.getDeptId());
        wrapper.eq("name", project.getName());
        wrapper.ne(ObjectUtil.isNotNull(project.getId()), "id", project.getId());
        int count = this.selectCount(wrapper);
        if (count > 0) {
            throw new RRException("同个公司下项目名称不能重复");
        }
    }
}
